﻿using BaseUtils.Helper;
using BaseUtils.Logger;
using Service.src.NW.Model;
using System;

namespace Service.src.Flow.JavaScript
{

    /// <summary>
    /// 事件
    /// 
    /// 用于通知界面
    /// </summary>
    public class JavaScriptEvent
    {

        private static readonly object _lock = new object();

        public static void MessageSuccess(string Message)
        {
            _Base(new Message(JavaScriptEventEnum.MESSAGE_SUCCESS, Message));
        }

        public static void MessageInfo(string Message)
        {
            _Base(new Message(JavaScriptEventEnum.MESSAGE_INFO, Message));
        }

        public static void MessageError(string Message)
        {
            _Base(new Message(JavaScriptEventEnum.MESSAGE_ERROR, Message));
        }

        public static void MessageWarning(string Message)
        {
            _Base(new Message(JavaScriptEventEnum.MESSAGE_WARNING, Message));
        }

        public static void Send(JavaScriptEventEnum _event, object data)
        {
            _Base(new Message(_event, data));
        }

        //public static void Send(JavaScriptEventEnum _event, NodeProcessor data)
        //{
        //    _Base(new Message(_event, data));
        //}

        public static void Send(JavaScriptEventEnum _event, string data)
        {
            _Base(new Message(_event, data));
        }

        private static object LockObject = new object();

        private static void _Base(Message Msg)
        {
            try
            {
                AppData._WebSocket.BroadcastMessage(Msg.ToJson(new[] { "Children", "Parents", "rect", "nodeInputParam" }));
            }
            catch (Exception ex)
            {
                LoggerService.Error($"后端推送消息失败，推送信息：{Msg}，错误原因：{ex.Message}", ex);
            }
        }

        public JavaScriptEvent() { }
    }

    public class Message
    {
        public JavaScriptEventEnum Type { get; set; }
        public object Data { get; set; }

        public Message(JavaScriptEventEnum _Type, object _Data)
        {
            Type = _Type;
            Data = _Data;
        }
    }

    /// <summary>
    /// 事件类型
    /// </summary>
    public enum JavaScriptEventEnum
    {
        NULL,
        UPDATE_FLOW_DATA,
        UPDATE_EVENT_DATA,
        GLOBAL_MESSAGE_NOTICE, // 全局的消息通知
        DEVICE_LOGGER,   // 监听设备日志
        FLOW_LOGGER,   // 流程日志
        FLOW_COMPLETE,  // 流程结束
        NODE_ERROR,   // 节点异常
        NODE_LOGGER,   // 节点日志
        NODE_STATUS,    // 节点状态
        NODE_START,  // 节点开始执行
        NODE_COMPLETE,  // 脚本编译错误
        NODE_SCRIPT_COMPILE_ERROR,  // 节点脚本编译出错
        GLOBAL_PARAM_DATA_UPDATE,  // 全局变量数据更新
        MESSAGE_INFO,  // 消息info
        MESSAGE_SUCCESS,  // 消息success
        MESSAGE_ERROR,  // 消息error
        MESSAGE_WARNING,  // 消息Warning
        SYSTEM_NOTIFICATION,  // 系统通知
    }

    public enum SystemNotificationEnum
    {
        REFRESH_PAGE,  // 刷新页面
        STOP_NOTIFICATION,  // 暂停页面滚到日志
    }
}
